// 获取url参数
export function getQueryString(name) { //decodeURI() 函数可对 encodeURI() 函数编码过的 URI 进行解码。
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]); return null;
}

// 从Array获取字典
export function getMap(array, key) {
    var returnObj = {};
    if ($.isArray(array)) {
        for (var i = 0; i < array.length; i++) {
            var item = array[i];
            returnObj[item[key]] = item;
        }
    }
    return returnObj;
}

// 返回日期时间的字符串转换
export function getStringDate(param, date) {
    var now = date ? date : new Date(), string = "";
    var year = now.getFullYear();
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var h = now.getHours();
    var m = now.getMinutes();
    var s = now.getSeconds();
    month = (month < 10 ? "0" : "") + month;
    day = (day < 10 ? "0" : "") + day;
    h = (h < 10 ? "0" : "") + h;
    m = (m < 10 ? "0" : "") + m;
    s = (s < 10 ? "0" : "") + s;
    switch (param) {
        case "day": string = year + "-" + month + "-" + day; break;
        case "h": string = year + "-" + month + "-" + day + " " + h; break;
        case "m": string = year + "-" + month + "-" + day + " " + h + ":" + m; break;
        case "s": string = year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s; break;
        case "filename": string = year + "" + month + "" + day + "" + h + "" + m + "" + s; break;
        default: string = year + "-" + month + "-" + day + " " + h + ":" + m + ":" + s; break;
    }
    return string;
}

// 数组添加判断某对象是否在其中，存在返回其索引和true，否则返回false
Array.prototype.objInArray = function (key, value) {
    //JSON.stringify(cfg.inputs).indexOf(JSON.stringify(taskNameObj)) != -1
    for (var i = 0; i < this.length; i++) {
        console.log(this[i][key])
        if (typeof (this[i]) == "object" && this[i][key].indexOf(value) > -1)
            return { result: true, index: i };
    }
    return false;
}

// 通过索引删除某项
Array.prototype.removeByIndex = function (index) {
    this.splice(index, 1);
};

// 通过某值删除某项，针对普通元素类型的数组
Array.prototype.removeByVal = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

// 是否在数组里存在
// rtindex:true 存在的时候返回索引 false 存在的时候返回true
export function is_in_array_kv(array, key, search, rtindex) {
    if (!$.isArray(array)) return false;
    for (var i in array) {
        if (array[i] && array[i][key] == search) {
            if (rtindex) return i;
            else return true;
        }
    }
    if (rtindex) return -1;
    else return false;
}

export function is_in_array(array, search, rtindex) {
    if (!$.isArray(array)) return false;
    for (var i in array) {
        if (array[i] == search) {
            if (rtindex) return i;
            else return true;
        }
    }
    if (rtindex) return -1;
    else return false;
}